<?php
// +----------------------------------------------------------------------
// | 整点秒杀-商品管理
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\facade\View;
use think\facade\Db;
class SeckillProduct extends Base
{
	//商品列表
    public function index(){
		if(request()->isAjax()){
			$page = input('param.page')?:1;
			$limit = input('param.limit')?:10;
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 's.w_time desc';
			}
			$where = array();
            $where[] = ['s.sid','=',1];
			if(input('param.name')) $where[] = ['s.name','like','%'.$_GET['name'].'%'];
			if(input('?param.status') && input('param.status')!=='') $where[] = ['s.status','=',input('param.status')];

			$count = 0 + Db::name('seckill_product')->alias('s')->join('goods g','s.goods_id=g.goods_id')->where($where)->count();
			$data = Db::name('seckill_product')->alias('s')->join('goods g','s.goods_id=g.goods_id')
                ->where($where)
                ->field('s.*,g.goods_name')
                ->page($page,$limit)
                ->order($order)->select()->toArray();
			foreach($data as $k=>$v){
				if($v['sid'] > 0){
					$data[$k]['sname'] = Db::name('suppliers')->where('id',$v['sid'])->value('name');
				}else{
					$data[$k]['sname'] = '平台自营';
				}
			}
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data]);
		}


		return View::fetch();
    }
	//编辑商品
	public function edit(){
        $info = [];
        $product = [];
		if(input('param.id')){
			$info = Db::name('seckill_product')->where('id',input('param.id/d'))->find();
            $info['stime'] = date('Y-m-d H:i:s',$info['stime']);
            $info['etime'] = date('Y-m-d H:i:s',$info['etime']);
            $product = Db::name('goods')->where('goods_id',$info['goods_id'])->find();
            $product['gglist'] = Db::name('spec_goods_price')->where('goods_id',$info['goods_id'])->select()->toArray();
            $product['price_arr'] = json_decode($info['price'],true);
		}
		View::assign('info',$info);
		View::assign('product',$product);
		return View::fetch();
	}
	//保存商品
	public function save(){
        $id = input('id/d');
		if($id){
			$product = Db::name('seckill_product')->where('id',$id)->find();
			if(!$product) {
                return json(['status'=>0,'msg'=>'秒杀不存在']);
            }
		}
		$info = input('post.info/a');
        $info['stime'] = strtotime($info['stime']);
        $info['etime'] = strtotime($info['etime']);
        if($info['etime']<time() || $info['etime']<$info['stime']){
            return json(['status'=>0,'msg'=>'请选择正确的秒杀时间']);
        }
        $price = input('price/a');
        $info['price'] = json_encode($price);
        $info['sid'] = SID?:1;
        $info['w_time'] = time();
        if($id){
            Db::name('seckill_product')->where('id',$id)->update($info);
        }else{
            $exit = Db::name('seckill_product')->where('goods_id',$info['goods_id'])->where('etime','>',time())->find();
            if($exit){
                return json(['status'=>0,'msg'=>'该商品存在未结束的秒杀活动']);
            }
            Db::name('seckill_product')->insert($info);
        }

		return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
	}
	//改状态
	public function setst(){
		$st = input('post.st/d');
		$ids = input('post.ids/a');
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['id','in',$ids];
		if(bid !=0){
			$where[] = ['bid','=',bid];
		}
		Db::name('seckill_product')->where($where)->update(['status'=>$st]);
		\app\common\System::plog('商城商品改状态'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//审核
	public function setcheckst(){
		$st = input('post.st/d');
		$id = input('post.id/d');
		$reason = input('post.reason');
		Db::name('seckill_product')->where('aid',aid)->where('id',$id)->update(['ischecked'=>$st,'check_reason'=>$reason]);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	
	//删除
	public function del(){
		$ids = input('post.ids/a');
		if(!$ids) $ids = array(input('post.id/d'));
		$where = [];
		$where[] = ['aid','=',aid];
		$where[] = ['id','in',$ids];
		if(bid !=0){
			$where[] = ['bid','=',bid];
		}
		$prolist = Db::name('seckill_product')->where($where)->select();
		foreach($prolist as $pro){
			Db::name('seckill_product')->where('id',$pro['id'])->delete();
			Db::name('seckill_guige')->where('proid',$pro['id'])->delete();
		}
		\app\common\System::plog('秒杀商品删除'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
	//复制商品
	public function procopy(){
		$product = Db::name('seckill_product')->where('aid',aid)->where('bid',bid)->where('id',input('post.id/d'))->find();
		if(!$product) return json(['status'=>0,'msg'=>'商品不存在,请重新选择']);
		$gglist = Db::name('seckill_guige')->where('aid',aid)->where('proid',$product['id'])->select()->toArray();
		$data = $product;
		$data['name'] = '复制-'.$data['name'];
		unset($data['id']);
		$data['status'] = 0;
		$newproid = Db::name('seckill_product')->insertGetId($data);
		foreach($gglist as $gg){
			$ggdata = $gg;
			$ggdata['proid'] = $newproid;
			unset($ggdata['id']);
			Db::name('seckill_guige')->insert($ggdata);
		}
		\app\common\System::plog('商城商品复制'.$newproid);
		return json(['status'=>1,'msg'=>'复制成功','proid'=>$newproid]);
	}
	//选择商品
	public function chooseproduct(){
		$sysset = Db::name('seckill_sysset')->where('aid',aid)->find();
        $systimeset = explode(',',$sysset['timeset']);

		View::assign('sysset',$sysset);
		View::assign('systimeset',$systimeset);
		return View::fetch();
	}
	//获取商品信息
	public function getproduct(){
		$proid = input('post.proid/d');
		$product = Db::name('seckill_product')->where('aid',aid)->where('id',$proid)->find();
		//多规格
		$newgglist = array();
		$gglist = Db::name('seckill_guige')->where('aid',aid)->where('proid',$product['id'])->select()->toArray();
		foreach($gglist as $k=>$v){
			$newgglist[$v['ks']] = $v;
		}
		$guigedata = json_decode($product['guigedata']);
		return json(['product'=>$product,'gglist'=>$newgglist,'guigedata'=>$guigedata]);
	}
}
